<?php

ob_start();
session_start();
include_once "../connect.php";
// รับค่ามาเก็บไว้ใน 
$emp_id = $_POST["txtEmpid"];
$txtNameTha = $_POST["txtNameTha"];
$txtsername = $_POST["txtsername"];
$txtDepartment = $_POST["txtDepartment"];
$requiteDatetime = $_POST["txtrequiteDatetime"];
$mydatetimeRequi =date('Y-m-d H:i:s', strtotime($requiteDatetime));
$txtCount = $_POST["txtCount"];
$txtProject = $_POST["txtProject"];
$txtDetails = $_POST["txtDetails"];
$grpId = $_POST["grpId"];
$mail = $_POST["mail"];
$txtde_id = $_POST["txtde_id"];


if (empty($grpId)) {
    $usermailServer = "itservice@adinop.co.th";
$passmailServer = "adinop123*";
include ("class.phpmailer.php");
function script_sendmail($to_name, $to_email, $from_name, $email_user_send, $email_pass_send, $subject, $body_html, $body_text) {

    $mail = new PHPMailer();
    $mail->From = $email_user_send;
    $mail->FromName = $from_name;

    $mail->AddAddress($to_email, $to_name);
    $mail->Subject = $subject;
    $mail->Body = $body_html;
    $mail->AltBody = $body_text;
    $mail->IsHTML(true);
    $mail->CharSet = "utf-8"; //กำหนด charset ของภาษาในเมล์ให้ถูกต้อง เช่น tis-620 หรือ utf-8

    $mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
 //  $mail->Host = 'smtp.gmail.com';
  //  $mail->Port = 465;
    $mail->SMTPAuth   = true;
//$mail->SMTPDebug  = true;
    $mail->Username = $email_user_send;
    $mail->Password = $email_pass_send;

    $mail->Send();
    $mail->ClearAddresses();
}
	   
$to_email_it	="suchart.nan@adinop.co.th";
$from_name_it	="$mail";
$email_user_send_it	="$usermailServer";
$email_pass_send_it	="$passmailServer";;
$subject_it	="$txtProject";
$body_html_it	="
<table width=\"550\" border=\"0\" cellspacing=\"1\" cellpadding=\"5\">
  <tr>
    <td colspan=\"2\">
<p style=\"font-size:16px; color:#660099\">รายละเอียด ใบคำขอออกแบบงานกราฟิก $txtDetails  <br />เพิ่มเติม  <a href=\"http://webportal/systemit/\">&nbsp;webportal/systemit</a></p>
 </td>
  </tr>
    <tr>
    <td colspan=\"2\"><div>
      <p>Regards,       </p>
      <p>IT Service Team.</p>
      <p>http://webportal/systemit/</p>
    </div></td>
  </tr>
</table>
";
//$body_text ="";

script_sendmail($to_name,$to_email_it,$from_name_it,$email_user_send_it,$email_pass_send_it,$subject_it,$body_html_it,$body_text);
if(script_sendmail){
  echo "SEND MAIL COMPLETE";
}
    
    
if (empty($txtProject)) {
    echo "โปรดกรอกข้อมูลให้ครบถ้วน";
} else {
    $name = $_FILES["txtFile"]["name"];
    $path = $_FILES["txtFile"]["tmp_name"];

    move_uploaded_file($path, "../upload/" . $name);
    
if(empty($grpId)){
    $newNum = mysql_result(mysql_query(" SELECT Max( grpNumber )+1 AS MaxID FROM graphic "),0,"MaxID"); 
    mysql_query($newNum);
    if ($newNum == NULL) { // ถ้าได้เป็นค่าว่าง หรือ null ก็แสดงว่ายังไม่มีข้อมูลในฐานข้อมูล
        $newNum = "1";
    } else {
        mysql_query($newNum); //ถ้าไม่ใช่ค่าว่าง
        echo $newNum;
    }
    
    $sqlInGra = "INSERT INTO `db_itmanager`.`graphic` (
`grpId` ,
`grpNumber` ,
`grpDate` ,
`grpReuiDate` ,
`grpnameTh` ,
`grpsurnameTh` ,
`grpNameProject` ,
`grpDetail` ,
`grpDateFinish` ,
`grpModel` ,
`grpExample` ,
`grpStatus` ,
`typeGrpId` ,
`emp_id` ,
`de_id`
)
VALUES (
NULL , '$newNum', NOW( ), '$mydatetimeRequi', '$txtNameTha', '$txtsername', '$txtProject', '$txtDetails', NOW( ), '$txtCount', '$name', 'wait', '1',  '$emp_id', '$txtde_id'
)";
   
    mysql_query($sqlInGra);
    
} else {

    header("location:\editGraphic.php");
}
if (mysql_query) {
    echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
    echo "<meta http-equiv='refresh' content='5; url=\altGraphic.php'>";
} else {
    echo mysql_error();
}
}
}
?>
